// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var databind = require("databind");
	var datas;
	var tools = require("tools");
	//	var _css = require("./login.css");

	var init = function() {
		initEvent();
	};
	var initEvent = function() {
		inquireCapitalDetail();
	};

	var inquireCapitalDetail = function() {
		base.fnAjax({
			action: "inquireCapitalDetail.json",
			async: true,
			nocode: true,
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					datas = data.datas[0];
					databind.fillForm($(".service-head"), datas);
					datas.d = [];
					for(var i = 0; i < datas.capitalKeys.length; i++) {
						var dd = {};
						dd.value = datas.capitalValues[i];
						dd.name = datas.capitalKeys[i];
						datas.d.push(dd);
					}
					showChart0();
					showChart1();
					showChart4();
					showChart6();
					
					$(".data[name=accountBalance]").html(tools.formatCurrency($(".data[name=accountBalance]").html()));
					$(".data[name=accountLoan]").html(tools.formatCurrency($(".data[name=accountLoan]").html()));
					$(".data[name=accountAmount]").html(tools.formatCurrency($(".data[name=accountAmount]").html()));
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var showChart0 = function() {
		var myChart0 = echarts.init(document.getElementById('main0'));
		//var app.title = '折柱混合';

		var option0 = {
			title: {
				text: '资金流入流出分析',
				subtext: '一润供应链',
				x: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['line', 'bar']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			legend: {
				x: 'center',
				y: 'bottom',
				data: ['流入金额', '流出金额', '净流入']
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: datas.dates
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '流入金额',
				type: 'bar',
				data: datas.receives
			}, {
				name: '流出金额',
				type: 'bar',
				data: datas.payments
			}, {
				name: '净流入',
				type: 'line',
				stack: '总量',
				label: {
					normal: {
						show: true,
						position: 'top'
					}
				},
				areaStyle: {
					normal: {}
				},
				data: datas.netInflow
			}]
		};
		// 使用刚指定的配置项和数据显示图表。
		myChart0.setOption(option0);
	};

	var showChart1 = function() {
		// 基于准备好的dom，初始化echarts实例
		var myChart1 = echarts.init(document.getElementById('main1'));

		// 指定图表的配置项和数据
		//app.title = '嵌套环形图';

		var option = {
			title: {
				text: '可用资金余额分析',
				subtext: '一润供应链',
				x: 'center'
			},
			tooltip: {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)"
			},
			legend: {
				orient: 'vertical',
				left: 'left',
				data: datas.capitalKeys
			},
			series: [{
				name: '访问来源',
				type: 'pie',
				radius: ['40%', '60%'],
				avoidLabelOverlap: false,
				center: ['50%', '60%'],
				data: datas.d,
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					}
				}
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart1.setOption(option);
		// 处理点击事件并且跳转到相应的百度搜索页面
		myChart1.on('click', function(params) {
			window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));
		});
	};

	var showChart4 = function() {
		// 基于准备好的dom，初始化echarts实例
		var myChart4 = echarts.init(document.getElementById('main4'));

		var option = {
			color: ['#3398DB'],
			title: {
				text: '资金流入月度趋势分析'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['资金流入']
			},
			toolbox: {
				feature: {
					saveAsImage: {}
				}
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: datas.rDate
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '资金流入',
				type: 'line',
				stack: '总量',
				label: {
					normal: {
						show: true,
						position: 'top'
					}
				},
				areaStyle: {
					normal: {}
				},
				data: datas.rAmount
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart4.setOption(option);
	};

	var showChart6 = function() {
		// 基于准备好的dom，初始化echarts实例
		var myChart6 = echarts.init(document.getElementById('main6'));

		var option = {
			title: {
				text: '资金流出月度趋势分析'
			},
			tooltip: {
				trigger: 'axis'
			},
			legend: {
				data: ['资金流出']
			},
			toolbox: {
				feature: {
					saveAsImage: {}
				}
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: datas.dDate
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '资金流出',
				type: 'line',
				stack: '总量',
				label: {
					normal: {
						show: true,
						position: 'top'
					}
				},
				areaStyle: {
					normal: {}
				},
				data: datas.dAmount
			}]
		};

		// 使用刚指定的配置项和数据显示图表。
		myChart6.setOption(option);
	};
	return {
		init: init
	};
});